import 'package:flutter/material.dart';

import '../../services/ScreenAdapter.dart';
import '../../services/UserInfo.dart';
import 'cart/CartItem.dart';

// 消息弹框
import 'package:fluttertoast/fluttertoast.dart';

// provider全局传值
import 'package:provider/provider.dart';
import '../../provider/Cart.dart';
import '../../provider/CheckOut.dart';

class CartPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CartPageState();
}

class CartPageState extends State {
  bool _isEdit = false;
  var cartProvider;
  var checkOutProvider;

  @override
  Widget build(BuildContext context) {
    // Provider存储的cart数据
    var cartProvider = Provider.of<Cart>(context);
    var checkOutProvider = Provider.of<CheckOut>(context);
    this.cartProvider = cartProvider;
    this.checkOutProvider = checkOutProvider;

    return Scaffold(
        appBar: AppBar(title: Text('购物车页面'), actions: [
          Container(
            margin: EdgeInsets.only(right: 20),
            child: InkWell(
              child: Row(
                children: [
                  Text('${this._isEdit ? "购买" : "删除"}',
                      style: TextStyle(
                        color: this._isEdit ? Colors.green : Colors.red,
                        fontWeight: FontWeight.w900,
                      )),
                  Icon(Icons.launch,
                      color: this._isEdit ? Colors.green : Colors.red)
                ],
              ),
              onTap: () {
                this.setState(() {
                  this._isEdit = !this._isEdit;
                });
              },
            ),
          )
        ]),
        body: cartProvider.cartList.length > 0
            ? Stack(
                children: [
                  Container(
                    height: double.infinity,
                    decoration: BoxDecoration(
                        color: Color.fromRGBO(200, 200, 200, 0.1),
                        border: Border(
                            bottom: BorderSide(
                                color: Colors.white,
                                width: ScreenAdapter.height(100)))),
                    child: ListView(
                      children: cartProvider.cartList.map((e){
                        return CartItem(item: e);
                      }).toList(),
                    ),
                  ),
                  Positioned(
                    width: ScreenAdapter.width(750),
                    bottom: 0,
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              top: BorderSide(
                            color: Colors.black12,
                            width: 1,
                          ))),
                      height: ScreenAdapter.height(100),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Checkbox(
                                activeColor: Colors.red,
                                value: cartProvider.isCheckedAll,
                                onChanged: (bool value) {
                                  // 反选
                                  cartProvider.checkAll(value);
                                },
                              ),
                              Text('全选')
                            ],
                          ),
                          this._isEdit ? Text('') : Text('总价：${cartProvider.allPrice}'),
                          this._isEdit
                              ? InkWell(
                                  child: Container(
                                    alignment: Alignment.center,
                                    width: ScreenAdapter.width(250),
                                    height: double.infinity,
                                    decoration: BoxDecoration(
                                        color: Colors.green,
                                        borderRadius: BorderRadius.circular(5)),
                                    child: Text('删除',
                                        style: TextStyle(color: Colors.white)),
                                  ),
                                  onTap: () {
                                    cartProvider.removeItem();
                                  },
                                )
                              : InkWell(
                                  child: Container(
                                    alignment: Alignment.center,
                                    width: ScreenAdapter.width(250),
                                    height: double.infinity,
                                    decoration: BoxDecoration(
                                        color: Colors.red,
                                        borderRadius: BorderRadius.circular(5)),
                                    child: Text('结算',
                                        style: TextStyle(color: Colors.white)),
                                  ),
                                  onTap: _checkOut,
                                )
                        ],
                      ),
                    ),
                  )
                ],
              )
            : Center(
                child: Text('购物车空的...'),
              ));
  }

  // 结算，判断用户是否登录，如果没有登录跳转到登录，如果已登录跳转到结算页面
  _checkOut() async {
    bool isLogin = await UserInfoServices.getUserLoginState();

    List cartList = await this.cartProvider.checkedTrueCartList;

    if (cartList.length > 0){

      // 将cart页面选中的数据，以及总价保存到checkOut页面provider中
      this.checkOutProvider.changeCheckOutListData(cartList);

      if(isLogin){
        Navigator.pushNamed(context, '/checkOut');
      }else {
        // 弹框提示
        Fluttertoast.showToast(
          msg: "您还未登录，请登录之后再结算！",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
        );
        Navigator.pushNamed(context,'/login');
      }
    }else {
      // 弹框提示
      Fluttertoast.showToast(
        msg: "您未选中商品！",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
    }


  }
}
